package com.company;

import java.io.*;
import java.util.ArrayList;

public class Animal implements Serializable {
    public String getName() {
        return name;
    }

    public String name = "animal";

    void eat() {
        System.out.println(this.getClass().getName());
    }

    public static void main(String[] args) {
        ArrayList<Animal> animals = new ArrayList<>();
        animals.add(new Dog());
        animals.add(new Cat());
        takeAnimals(animals);
        saveFile(animals);

    }

    public static void takeAnimals(ArrayList<? extends Animal> animals) {
        for (Animal a : animals) {
            a.eat();
        }
    }

    static void saveFile(final ArrayList<? extends Animal> animals) {
        try {
            File file = new File("animals");
            BufferedWriter writer = new BufferedWriter(new FileWriter(file));
            for (Animal animal : animals) {
                writer.write(animal.getName() + "\n");
            }
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

class Dog extends Animal {
    public String name = "Dog";

    public String getName() {
        return name;
    }

    void eat() {
        System.out.println("Dog eat");
    }
}

class Cat extends Animal {
    public String name = "Cat";

    public String getName() {
        return name;
    }

    void eat() {
        System.out.println("Cat eat");
    }
}
